package com.sporadic.desk.controller;

import bo.KeepOrderDetailsBO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.sporadic.desk.pojo.Order;
import com.sporadic.desk.pojo.OrderDetails;
import com.sporadic.desk.service.*;
import dto.OrderDTO;
import dto.OrderDetailsDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import vo.OrderEchoVO;
import vo.ResponseData;

import java.util.List;
import java.util.Map;

/**
 * @program: sporadic
 * @description:
 * @author: LHD   QQ:1727865856
 * @create: 2019-10-24 10:20
 **/
@CrossOrigin(value = "http://localhost:8080")
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailsService detailsService;
    @Autowired
    private PayService payService;
    @Autowired
    private UserCouponService userCouponService;

    @PostMapping("/addOrder")
    public ResponseData<Void> add(@RequestBody OrderDTO orders) {
        orderService.addOrder(orders);
        return new ResponseData<>(null);
    }

    @PostMapping("/addOrderDetails")
    public ResponseData<Void> keepOrderDetails(@RequestBody KeepOrderDetailsBO keepOrderDetailsBO) {
        detailsService.keepOrderDetails(keepOrderDetailsBO);
        return new ResponseData<>(null);
    }

    @GetMapping("/queryOrder")
    public ResponseData<OrderEchoVO> query(Integer current, Integer deskNum, Integer mid) {
        //查询订单状态为1的某个门店,某个餐桌的订单 ：因为每个门店每个餐桌只会同时又一个订单存在。所以这样就保证了单一性
        OrderEchoVO echoVO = orderService.queryOrder(current, deskNum, mid, 1);
        return new ResponseData<>(echoVO);
    }

    //支付成功之后修改订单状态
    @GetMapping("/updateOrderStauts")
    public ResponseData<Void> pay(String orderId) {
        boolean resultValue = orderService.updateOrderStatus(orderId);
        if (resultValue) {
            return new ResponseData<>(null);
        } else {
            return new ResponseData<>(null, "订单状态修改失败！");
        }
    }

    @GetMapping("/queryAlldeatils")
    public ResponseData<List<OrderDetailsDTO>> queryall(String orderNo) {
        List<OrderDetailsDTO> orderAllDetails = orderService.queryAll(orderNo);
        return new ResponseData<>(orderAllDetails);
    }

    @GetMapping("/queryorderByid")
    public ResponseData<OrderDTO> queryByid(String orderId) {
        return orderService.queryByid(orderId);
    }

    @PostMapping("/QrcodePay")
    public ResponseData<String> quCodePay(@RequestBody String jsonVal) {
        String orderno = JSON.parseObject(jsonVal).getString("orderNo");
        ResponseData<String> qrpay = payService.qrpay(orderno);
        return qrpay;
    }

    @PostMapping("/sporadicpay")
    public ResponseData<Void> sporadicPay(@RequestBody String Jsonparam) {
        JSONObject parseObject = JSON.parseObject(Jsonparam);
        String paymentCode = parseObject.getString("paymentCode");
        String orderNo = parseObject.getString("orderNo");
        ResponseData<String> payValue = payService.alipay(paymentCode, orderNo);
        switch (payValue.getStatusCode()) {
            case 500:
                return new ResponseData<>(null, payValue.getMsg());
            default:
                //支付成功，修改订单状态,餐桌状态
                orderService.updateOrderStatus(orderNo);
                return new ResponseData<>(null);
        }
    }

    @GetMapping("/querypay")
    public ResponseData<String> queryPay(String orderNo) {
        ResponseData<String> ResponseData = payService.queryQrpay(orderNo);
        System.out.println("查询支付返回状态码:"+ResponseData.getStatusCode());
        if (ResponseData.getStatusCode() == 200) {
            try{
                orderService.updateOrderStatus(orderNo);
                return new ResponseData<>(null);
            }catch (Exception e){
                throw new RuntimeException("支付成功，餐桌状态异常！");
            }
        } else {
            return new ResponseData<>(null, "付款未完成");
        }
    }
    @GetMapping("/queryBymidAllorder")
    public ResponseData<List<OrderDTO>> queryBymidAllorder(Integer mid){
        return new ResponseData<>(orderService.queryBymid(mid));
    }

    @PostMapping("/updateOrder")
    public ResponseData<Boolean> updateOrder(String json){
        return orderService.updateOrder(json);
    }

}
